package cn.echoedu.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class DbUtil {
	private Connection conn=null;
	private String url="jdbc:mysql://localhost:3306/dbaichewei?useUnicode=true&characterEncoding=utf-8&autoReconnect=true";
	private String user="root";
	private String password="123456";
	private Statement stmt=null;
	private ResultSet rs=null;
	private Connection getConn(){
		try {
			Class.forName("com.mysql.jdbc.Driver");
			conn=DriverManager.getConnection(url, user, password);
			return conn;
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;

	}
	public ResultSet executeQuery(String sql) throws SQLException{
		stmt=null;
		rs=null;
		try {
			stmt=getConn().createStatement();
			rs=stmt.executeQuery(sql);
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		return rs;
	}
	public boolean executeUpdate(String sql){
		stmt=null;
		try{
			stmt=getConn().createStatement();
			int flag=stmt.executeUpdate(sql);
			if(flag==1){
				return true;
			}
		}catch(SQLException e){
			e.printStackTrace();
		}
		return false;
	}
	public void closeStmt(){
		try{
			stmt.close();
		}catch(SQLException e){
			e.printStackTrace();
		}
	}
	public void closeConn(){
		try{
			getConn().close();
		}catch(SQLException e){
			e.printStackTrace();
		}
	}
	public static void rollback(Connection con) throws SQLException{
		 
			con.rollback();  
		 
	}
}
